package com.ltcode.sys.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltcode.sys.domain.model.SysDept;
import com.ltcode.sys.domain.repository.SysDeptRepository;
import com.ltcode.sys.infrastructure.persistence.converter.SysDeptConverter;
import com.ltcode.sys.infrastructure.persistence.entity.SysDeptPO;
import com.ltcode.sys.infrastructure.persistence.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 部门仓储实现
 * 
 * @author ltcode
 * @since 2024-01-01
 */
@Repository
public class SysDeptRepositoryImpl implements SysDeptRepository {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<SysDept> selectDeptList(SysDept dept) {
        LambdaQueryWrapper<SysDeptPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dept.getDeptName()), SysDeptPO::getDeptName, dept.getDeptName())
                .eq(StringUtils.hasText(dept.getStatus()), SysDeptPO::getStatus, dept.getStatus())
                .orderByAsc(SysDeptPO::getParentId)
                .orderByAsc(SysDeptPO::getOrderNum);
        
        List<SysDeptPO> poList = sysDeptMapper.selectList(queryWrapper);
        return SysDeptConverter.toDomainList(poList);
    }

    @Override
    public List<Long> selectDeptListByRoleId(Long roleId, boolean deptCheckStrictly) {
        return sysDeptMapper.selectDeptListByRoleId(roleId, deptCheckStrictly);
    }

    @Override
    public SysDept selectDeptById(Long deptId) {
        SysDeptPO po = sysDeptMapper.selectDeptById(deptId);
        return SysDeptConverter.toDomain(po);
    }

    @Override
    public int selectNormalChildrenDeptById(Long deptId) {
        List<SysDeptPO> children = sysDeptMapper.selectNormalChildrenDeptById(deptId);
        return children != null ? children.size() : 0;
    }

    @Override
    public boolean hasChildByDeptId(Long deptId) {
        int result = sysDeptMapper.hasChildByDeptId(deptId);
        return result > 0;
    }

    @Override
    public boolean checkDeptExistUser(Long deptId) {
        int result = sysDeptMapper.checkDeptExistUser(deptId);
        return result > 0;
    }

    @Override
    public boolean checkDeptNameUnique(SysDept dept) {
        Long deptId = dept.getId() == null ? -1L : dept.getId();
        SysDeptPO info = sysDeptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
        if (info != null && info.getId().longValue() != deptId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public int insertDept(SysDept dept) {
        SysDeptPO po = SysDeptConverter.toPO(dept);
        return sysDeptMapper.insert(po);
    }

    @Override
    public int updateDept(SysDept dept) {
        SysDeptPO po = SysDeptConverter.toPO(dept);
        return sysDeptMapper.updateById(po);
    }

    @Override
    public int deleteDeptById(Long deptId) {
        return sysDeptMapper.deleteById(deptId);
    }

    @Override
    public void updateDeptStatusNormal(Long[] deptIds) {
        for (Long deptId : deptIds) {
            SysDeptPO po = new SysDeptPO();
            po.setId(deptId);
            po.setStatus("0");
            sysDeptMapper.updateById(po);
        }
    }
}
